// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-20 15:41
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.ArraysTrain;

public class Rotate {
    /**
     * 在空间复杂度为O(1)的情况下实现图像的旋转
     * 根据旋转公式进行图像节点的交换 -> （i，j） - > (j,n-i-1)
     *
     * @param matrix: 图像数据
     */
    public void rotate(int[][] matrix) {
        int n = matrix.length;
        int h = 0, t = n - 1, layer = 0;
        while (h < t && layer < n) {
            for (int i = h; i < t; i++) {
                roll(matrix, i, layer);
            }
            layer++;
            h++;
            t--;
        }
    }

    /**
     * 旋转当前层的数据
     *
     * @param matrix
     * @param col
     * @param layer
     */
    private void roll(int[][] matrix, int col, int layer) {
        int tmp = matrix[layer][col], time = 3, n = matrix.length;
        int x = layer, y = col;
        while (time > 0) {
            matrix[x][y] = matrix[n - (y + 1)][x];
            int change = x;
            x = n - (y + 1);
            y = change;
            time--;
        }
        matrix[x][y] = tmp;
    }
}
